<template>
    <div>
        <el-dialog :title="isAdd ? '添加商品分类' : '编辑商品分类'" :visible.sync="isShow" center :before-close="cancel">
            <el-form :model="cateForm" ref="userinfo" :rules="rules">
                <el-form-item prop="pid" label="上级分类" :label-width="formLabelWidth">
                    <el-select v-model="cateForm.pid" placeholder="请选择上级分类">
                        <el-option label="顶级分类" :value="0"></el-option>
                        <el-option v-for="item in catelist" :key="item.id" :value="item.id" :label="item.catename">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="catename" label="分类名称" :label-width="formLabelWidth">
                    <el-input v-model="cateForm.catename" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload 
                        action="#" 
                        list-type="picture-card"
                        :auto-upload="false"
                        :on-preview="onPreview"
                        :on-remove="onRemove"
                        :on-change="onChange"
                        :file-list="fileList"
                        :on-exceed="handleExceed"
                        :limit="1"
                    >
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="cateForm.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { cateAdd,getCateInfo,cateEdit } from '../../request/api'
import { mapActions, mapGetters } from "vuex";
export default {
    props: ['isShow', 'isAdd'],
    data() {
        return {
            formLabelWidth: '120px',
            cateForm: {
                pid: 0,
                catename: '',
                img: '',
                status: 1
            },
            rules: {
                catename: [
                    { required: true, message: "请输入分类名称", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
                ],
                pid: [{ required: true, message: "请选择上级分类", trigger: "change" }],
            },
            dialogVisible: false,//控制图片预览的显示隐藏
            dialogImageUrl: '',//图片地址
            fileList:[] //文件列表
        }
    },
    computed: {
        ...mapGetters({
            catelist: "cate/getCateList",
        }),
    },
    methods: {
        //超出文件上传的回调事件
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择1个文件，本次选择了${files.length}个文件，共选择了${files.length+fileList.length}个文件`)
        },
        //图片改变时发生的事件
        onChange(file, fileList) {
            this.cateForm.img = file.raw
        },
        //图片预览
        onPreview(file) { 
            //显示弹框
            this.dialogVisible = true
            this.dialogImageUrl = file.url
        },
        //图片移除
        onRemove(file,fileList){},
        ...mapActions({
            getCate: "cate/changeCateAction",
        }),
        //取消
        cancel() {
            this.$emit('cancel', false)
            this.reset()
        },
        reset() {
            this.cateForm = {
                pid: 0,
                catename: "",
                img: "",
                status: 1, 
            };
            //清空表单验证
            this.$refs.userinfo.resetFields();
            //清空上传文件
            this.fileList = []
        },

        lookup(id) {
            getCateInfo({ id }).then(res => {
                if (res.code == 200) {
                    this.cateForm = res.list
                    this.cateForm.id = id
                    this.fileList = this.cateForm.img?[{url:'http://localhost:3000'+this.cateForm.img}]:[]
                }
            })
        },
        //确定提交
        confirm() {
            this.$refs.userinfo.validate(val => {
                if (val) {
                    if (this.isAdd) {
                        //添加流程
                        cateAdd(this.cateForm).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                // this.$store.dispatch('cate/changeCateAction')
                                this.cancel()
                                this.getCate()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        //编辑流程
                        cateEdit(this.cateForm).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.cancel()
                                this.getCate()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>

<style scoped>

</style>